#include "ClientControlResponder.h"

#include <iostream>

void ClientControlResponder::alertToInput( const InputSet& input ){
	if(input.get( IS_U )){
		if(!isUp){
			keysChanged = 1;
		}
		isUp = 1;
	}
	else{
		if(isUp){
			keysChanged = 1;
		}
		isUp = 0;
	}
	if(input.get( IS_D )){
		if(!isDown){
			keysChanged = 1;
		}
		isDown = 1;
	}
	else{
		if(isDown){
			keysChanged = 1;
		}
		isDown = 0;
	}
	if(server){
		if(input.get( IS_L )){
			if(!isLeft){
				keysChanged = 1;
			}
			isLeft = 1;
		}
		else{
			if(isLeft){
				keysChanged = 1;
			}
			isLeft = 0;
		}
		if(input.get( IS_R )){
			if(!isRight){
				keysChanged = 1;
			}
			isRight = 1;
		}
		else{
			if(isRight){
				keysChanged = 1;
			}
			isRight = 0;
		}
	}
	else{
		if(input.get( IS_R )){
			if(!isLeft){
				keysChanged = 1;
			}
			isLeft = 1;
		}
		else{
			if(isLeft){
				keysChanged = 1;
			}
			isLeft = 0;
		}
		if(input.get( IS_L )){
			if(!isRight){
				keysChanged = 1;
			}
			isRight = 1;
		}
		else{
			if(isRight){
				keysChanged = 1;
			}
			isRight = 0;
		}
	}
}

void ClientControlResponder::updateState(){
	if(keysChanged){
		//std::cout << "keysChanged\n";
		keysChanged = 0;
		buffer[0] = (char)isUp;
		buffer[1] = (char)isDown;
		buffer[2] = (char)isRight;
		buffer[3] = (char)isLeft;
		handler->addBroadcastMessage(buffer, 4, tgt);
	}
}
